package com.zebra.common;

import io.netty.buffer.ByteBuf;

import java.io.UnsupportedEncodingException;

public class ByteBufUtils {

    public static byte[] readBytes(ByteBuf b) {
        int realLength = b.readableBytes();
        if (realLength < 1){
            return null;
        }
        byte[] bytes = new byte[realLength];
        b.readBytes(bytes);
        return bytes;
    }

    public static byte readByte(ByteBuf b) {
        return b.readByte();
    }

    public static byte[]  readByteArray(ByteBuf b){
        int length = b.readInt();
        if (length < 1){
            return null;
        }

        byte[] bytes = new byte[length];
        for (int i=0; i<length; i++){
            bytes[i] = b.readByte();
        }
        return bytes;
    }



    public static short readShort(ByteBuf b) {
        return b.readShort();
    }

    public static short[] readShortArray(ByteBuf b) {
        int length = b.readInt();
        if (length < 1){
            return null;
        }
        short[] shorts = new short[length];
        for (int i=0; i<length; i++){
            shorts[i] = b.readShort();
        }
        return shorts;
    }

    public static float readFloat(ByteBuf b) {
        return b.readFloat();
    }

    public static float[] readFloatArray(ByteBuf b) {
        int length = b.readInt();
        if (length < 1){
            return null;
        }
        float[] floats = new float[length];
        for (int i=0; i<length; i++){
            floats[i] = b.readFloat();
        }
        return floats;
    }

    public static double readDouble(ByteBuf b) {
        return b.readDouble();
    }

    public static double[] readDoubleArray(ByteBuf b) {
        int length = b.readInt();
        if (length < 1){
            return null;
        }
        double[] doubles = new double[length];
        for (int i=0; i<length; i++){
            doubles[i] = b.readDouble();
        }
        return doubles;
    }


    public static int readInt(ByteBuf b) {
        return b.readInt();
    }

    public static int[] readIntArray(ByteBuf b) {
        int length = b.readInt();
        if (length < 1){
            return null;
        }
        int[] ints = new int[length];
        for (int i=0; i<length; i++){
            ints[i] = b.readInt();
        }
        return ints;
    }


    public static long readLong(ByteBuf b) {
        return b.readLong();
    }

    public static long[] readLongArray(ByteBuf b) {
        int length = b.readInt();
        if (length < 1){
            return null;
        }
        long[] longs = new long[length];
        for (int i=0; i<length; i++){
            longs[i] = b.readLong();
        }
        return longs;
    }


    public static String readString(ByteBuf b) {
        int length = b.readInt();
        if (length <= 0)
            return null;
        if (b.readableBytes() < length)
            return null;
        byte[] bytes = new byte[length];
        b.readBytes(bytes);
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }


    public static String[] readStringArray(ByteBuf b){
        int length = b.readInt();
        if (length < 1){
            return null;
        }
        String[] strings = new String[length];
        for (int i=0; i<length; i++){
            strings[i] = readString(b);
        }
        return strings;
    }


    /*** -----------------Write------------------------------------ */
    public static void writeBytes(ByteBuf b, byte[] v) {
        if (v == null) {
            return;
        }
        b.writeBytes(v);
    }

    public static void writeByte(ByteBuf b, byte v) {
        b.writeByte(v);
    }
    public static void writeByteArray(ByteBuf b, byte[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (byte  v : vs){
            b.writeByte(v);
        }

    }


    public static void writeShort(ByteBuf b, short v) {
        b.writeShort(v);
    }

    public static void writeShortArray(ByteBuf b, short[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (short  v : vs){
            b.writeShort(v);
        }
    }


    public static void writeInt(ByteBuf b, int v) {
        b.writeInt(v);
    }

    public static void writeIntArray(ByteBuf b, int[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (int  v : vs){
            b.writeInt(v);
        }
    }

    public static void writeLong(ByteBuf b, long v) {
        b.writeLong(v);
    }
    public static void writeLongArray(ByteBuf b, long[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (long  v : vs){
            b.writeLong(v);
        }
    }

    public static void writeFloat(ByteBuf b, float v) {
        b.writeFloat(v);
    }

    public static void writeFloatArray(ByteBuf b, float[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (float  v : vs){
            b.writeFloat(v);
        }
    }


    public static void writeDouble(ByteBuf b, double v) {
        b.writeDouble(v);
    }

    public static void writeDoubleArray(ByteBuf b, double[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (double  v : vs){
            b.writeDouble(v);
        }
    }

    public static void writeString(ByteBuf b, String v) {
        if (v == null) {
            b.writeInt(0);
            return;
        }

        try {
            byte[] bytes = v.getBytes("UTF-8");
            b.writeInt(bytes.length);
            b.writeBytes(bytes);
        } catch (UnsupportedEncodingException e) {
            b.writeInt(0);
        }
    }

    public static void writeStringArray(ByteBuf b, String[] vs) {
        if (vs == null || vs.length == 0){
            b.writeInt(0);
            return;
        }
        b.writeInt(vs.length);
        for (String  v : vs){
            writeString(b, v);
        }
    }


}
